/*
package com.roncoo.config;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.roncoo.entity.ReturnData;
import com.roncoo.util.JwtUtil;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

*/
/**
 * @author:袁帅强
 * @Description
 * @date:2021/3/18
 *//*

@Component
public class LoginFilter implements GlobalFilter, Ordered {
    @Value("${my.jwt.skip-auth-urls}")
    private String skipAuthUrls;
    @Resource
    private ObjectMapper objectMapper;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String url = exchange.getRequest().getURI().getPath();//从前台传过来的访问路径
        //System.out.println("请求的url路径是"+url);
        String[] split = skipAuthUrls.split(",");//分隔为数组
        List<String> strings = Arrays.asList(split);
        //无需验证，直接放行
        if (split!=null && strings.contains(url)){
            System.out.println(2+"放行了路径"+url);
            return chain.filter(exchange);
        }else {
            //携带的头部的Authorization的参数信息
            String token = exchange.getRequest().getHeaders().getFirst("Authorization");
            //获取Response对象
            ServerHttpResponse resp = exchange.getResponse();
            System.out.println(url+"3=token的值为："+token);
            //System.out.println("null".equals(token));
            if (StringUtils.isBlank(token)){
                //没有token 失败 return Data;
                return authErro(resp,"请先登录！！！",401,null);
            }else {
                //System.out.println(4);
                //有token 进行验证 是否失效 如果失效 会抛出一个异常ExpiredJwtException
                try {
                    JwtUtil.checkToken(token, objectMapper);
                    return chain.filter(exchange);
                } catch (ExpiredJwtException e) {
                    System.out.println(e.getMessage());
                    //log.error(e.getMessage(),e);
                    if (e.getMessage().contains("Allowed clock skew")) {
                        return authErro(resp, "认证过期,请重新登录！！！",402,null);
                    } else {//不是过期而是其他异常，认证失败
                        System.out.println("有一场的认证失败！！！！");
                        return authErro(resp, "认证失败",401,null);
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    ///log.error(e.getMessage(),e);
                    return authErro(resp, "认证失败",401,null);
                }
            }
        }
        //判断该路径是否需要放行，
        //如果是，放行走下面的过滤器，
        //如果不是就走认证 token
        //token==null，不能通过
        //token有时效性，过期不能通过
        //token可以伪造，假的不能通过
    }

    private Mono<Void> authErro(ServerHttpResponse resp, String mess,Integer code,String data) {
        //resp.setStatusCode(HttpStatus.UNAUTHORIZED);
        resp.getHeaders().add("Content-Type","application/json;charset=UTF-8");
        ReturnData<String> returnData = new ReturnData(code, mess, data,null,null,null);
        String returnStr = "";
        try {
            returnStr = objectMapper.writeValueAsString(returnData);
        } catch (JsonProcessingException e) {
            //log.error(e.getMessage(),e);
        }

        DataBuffer buffer = resp.bufferFactory().wrap(returnStr.getBytes(StandardCharsets.UTF_8));
        return resp.writeWith(Flux.just(buffer));
    }
    //优先级 值越小优先级越高
    @Override//优先级
    public int getOrder() {
        return -1;
    }
}
*/
